class Solution {
public:
    bool isBalanced(TreeNode* root) 
    {
        if(root == NULL)
            return true;
        
        int leftDepth = getDepth(root -> left);
        int rightDepth = getDepth(root -> right);

        if(abs(leftDepth - rightDepth) > 1)
            return false;
        else
            return isBalanced(root -> left) && isBalanced(root -> right);

    }

    int getDepth(TreeNode* root)
    {
        if(root == NULL)
            return 0;
            
        int leftDepth = getDepth(root -> left);
        int rightDepth = getDepth(root -> right);

        return max(leftDepth, rightDepth) + 1;
    }
};
